package com.qujia.station.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.qujia.common.utils.StringUtils;
import com.qujia.station.domain.PayOrder;
import com.qujia.station.domain.PayWay;
import com.qujia.station.domain.SaasOrderBo;
import com.qujia.station.domain.Station;
import com.qujia.station.domain.vo.SaasOrderVO;
import com.qujia.station.mapper.PayWayMapper;
import com.qujia.station.mapper.SettleInfoRecordMapper;
import com.qujia.station.mapper.StationMapper;
import com.qujia.station.rocketmq.SettleInfoProducer;
import com.qujia.station.service.SettleInfoPushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class SettleInfoPushServiceImpl implements SettleInfoPushService {

    @Resource
    private PayWayMapper payWayMapper;

    @Resource
    private StationMapper stationMapper;

    @Resource
    private SettleInfoProducer settleInfoProducer;

    @Resource
    private SettleInfoRecordMapper settleInfoRecordMapper;


    @Override
    @Async
    public void pushSettleInfo(PayOrder payOrder, JSONObject jsonObj) {
        log.info("开始推送退款订单清分信息。订单{}：",payOrder.toString());
        //查询mno
        Long payWayId = payOrder.getPayWayId();
        PayWay payWay = payWayMapper.selectPayWayById(Long.valueOf(payWayId));

        log.info("payWay----{}",payWay.getMno());

        Long stationId = payOrder.getStationId();
        Station station = stationMapper.selectStationById(stationId);
        if(StringUtils.isEmpty(station.getClearAccount()) || StringUtils.isEmpty(station.getClearMerMo()) ){
            return;
        }

        //营销订单号
        String marketingOrderNo = jsonObj.getString("marketingOrderNo");
        BigDecimal actualAmt = payOrder.getActualAmt();
        if (actualAmt == null) {
            actualAmt = payOrder.getAmt();
        }

        log.info("station----{},{}",station.getClearAccount(),station.getClearMerMo());
        if(StringUtils.isNotEmpty(station.getClearAccount()) && StringUtils.isNotEmpty(station.getClearMerMo())){
            SaasOrderBo saasOrderBo = SaasOrderBo.builder()
                    .payOrder(jsonObj.getString("refundTxnOrderNo"))
                    .srcPayOrder(jsonObj.getString("srcPayOrderNo"))
                    .payMerNo(payWay.getMno())
                    .payTime(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN))
                    .orderType("02")
                    .orderAmt(Objects.nonNull(actualAmt) ? actualAmt : BigDecimal.ZERO)
                    .payAmt(new BigDecimal(jsonObj.getString("refundAmt")))
                    .marketingOrder(StringUtils.isNotBlank(marketingOrderNo) ? marketingOrderNo : "")
                    .totalDisAmt(new BigDecimal(jsonObj.getString("marketingDisAmt")))
                    .merchantDisAmt(new BigDecimal(jsonObj.getString("merchantDisAmt")))
                    .platformDisAmt(new BigDecimal(jsonObj.getString("platformDisAmt")))
                    .sponsorDisAmt(new BigDecimal(jsonObj.getString("sponsorDisAmt")))
                    .clearAccount(station.getClearAccount())
                    .clearMerNo(station.getClearMerMo())
                    .optAmt(new BigDecimal("0")).build();
            log.info("saasOrderBo==={}",JSONObject.toJSONString(saasOrderBo));
            settleInfoProducer.sendMessage(saasOrderBo);

            //保存收银台信息
            //保存收银台信息
//            insertYunhuaOrderInfo(YunhuaOrderPayInfo.builder()
//                    .payOrderNo(jsonObj.getString("srcPayOrderNo"))
//                    .marketingDisAmt(jsonObj.getString("platformDisAmt"))
//                    .orderNo(payOrder.getOrderNo())
//                    .platformDisAmt(jsonObj.getString("platformDisAmt"))
//                    .merchantDisAmt(jsonObj.getString("merchantDisAmt"))
//                    .sponsorDisAmt(jsonObj.getString("sponsorDisAmt"))
//                    .payAmt(jsonObj.getString("refundAmt"))
//                    .refundPayOrderNo(jsonObj.getString("refundTxnOrderNo"))
//                    .type(1)
//                    .build());

        }
    }

    @Override
    public void handMovementPushSettleInfo(SaasOrderVO saasOrderVO) {
    	log.info("开始手动推送退款订单清分信息。订单{}：",saasOrderVO.toString());
        SaasOrderBo saasOrderBo = SaasOrderBo.builder()
                .payOrder(saasOrderVO.getPayOrder() )
                .srcPayOrder(saasOrderVO.getSrcPayOrder())
                .payMerNo(saasOrderVO.getPayMerNo())
                .payTime(saasOrderVO.getPayTime())
                .orderType("02")
                .orderAmt(saasOrderVO.getOrderAmt())
                .payAmt(saasOrderVO.getPayAmt())
                .marketingOrder(saasOrderVO.getMarketingOrder())
                .totalDisAmt(saasOrderVO.getTotalDisAmt())
                .merchantDisAmt(saasOrderVO.getMerchantDisAmt())
                .platformDisAmt(saasOrderVO.getPlatformDisAmt())
                .sponsorDisAmt(saasOrderVO.getSponsorDisAmt())
                .clearAccount(saasOrderVO.getClearAccount())
                .clearMerNo(saasOrderVO.getClearMerNo())
                .optAmt(saasOrderVO.getOptAmt()).build();
    	settleInfoProducer.sendMessage(saasOrderBo);
    	log.info("手动推送退款订单清分信息结束");
    }
    
//    @Override
//    public void insertYunhuaOrderInfo(YunhuaOrderPayInfo yunhuaOrderPayInfo) {
//        yunhuaOrderPayInfoMapper.insertYunhuaOrderPayInfo(yunhuaOrderPayInfo);
//    }
}
